package com.weixing.mall.provider.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.provider.mapper.ProductDevelopMapper;
import com.weixing.mall.provider.mapper.ProjectInfoMapper;
import com.weixing.mall.provider.mapper.ProjectOperationMapper;
import com.weixing.mall.provider.model.domain.ProductDevelop;
import com.weixing.mall.provider.model.domain.ProjectInfo;
import com.weixing.mall.provider.model.domain.ProjectOperation;
import com.weixing.mall.provider.model.dto.pdm.ProductDevelopDto;
import com.weixing.mall.provider.model.query.ProductDevelopQuery;
import com.weixing.mall.provider.model.vo.pdm.ProductDevelopFlowVo;
import com.weixing.mall.provider.model.vo.pdm.ProductDevelopVo;
import com.weixing.mall.provider.service.IProductDevelopService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @ClassName ProductDevelopServiceImpl
 * @Description TODO <br/>
 * @Author gjq
 * @Modified By:
 * @Date 2019/9/10
 * @Version 1.0
 **/
/**
 * <p>
 * 产品研发表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-18
 */
@Service
public class ProductDevelopServiceImpl extends ServiceImpl<ProductDevelopMapper, ProductDevelop> implements IProductDevelopService {

    @Autowired
    private ProjectInfoMapper projectInfoMapper;
    @Autowired
    private ProjectOperationMapper projectOperationMapper;

    @Override
    public IPage<ProductDevelopVo> selectListOfPage(Page page, ProductDevelopQuery productDevelopQuery) {
        return baseMapper.selectListOfPage(page,productDevelopQuery);
    }

    @Override
    public ProductDevelopVo getProductDevelopById(Long id) {
        return baseMapper.getProductDevelopById(id);
    }

    @Override
    public void save(ProductDevelopDto productDevelopDto,Long uid) {
        ProductDevelop productDevelop = new ProductDevelop();
        BeanUtils.copyProperties(productDevelopDto,productDevelop);
        if(productDevelopDto.getSeason()!=null){
            productDevelop.setSeason(productDevelopDto.getSeason().getValue());
        }
        if(productDevelopDto.getProductBand()!=null){
            productDevelop.setProductBand(productDevelopDto.getProductBand().getValue());
        }

        if (null == productDevelopDto.getId()){
            baseMapper.insert(productDevelop);

            //设置维护信息
            saveOperation(productDevelop.getId(),"新增产品信息",uid);

            //新增时，添加projectInfo信息，以便进行之后让别的部门进行修改
            ProjectInfo projectInfo = new ProjectInfo();
            projectInfo.setProjectId(productDevelop.getId());
            projectInfoMapper.insert(projectInfo);
        }else{
            productDevelop.setUpdatedTime(new Date());
            updateById(productDevelop);
            //设置维护信息
            saveOperation(productDevelop.getId(),"修改产品信息",uid);
        }
    }

    @Override
    public IPage<ProductDevelopFlowVo> selectFlowList(Page page,ProductDevelopQuery productDevelopQuery) {

        return baseMapper.selectFlowList(page,productDevelopQuery);
    }

    private void saveOperation(Long projectId,String remark,Long uid){
        ProjectOperation projectOperation = new ProjectOperation();
        projectOperation.setCreatedBy(uid);
        projectOperation.setCreatedTime(new Date());
        projectOperation.setProductId(projectId);
        projectOperation.setRemark(remark);
        projectOperationMapper.insert(projectOperation);
    }
}
